Release 10.1A: OpenEdge Data Management:
SQL Development


Writing stored procedures

Use any text editor to write the CREATE PROCEDURE statement and save the source text as a text file. That way, you can easily modify the source text and try again if it generates syntax or Java compilation errors.

From the command prompt, you can invoke SQL Explorer and submit the file containing the CREATE PROCEDURE statement as an input script, as shown in Example 9–5.

Example 9–5: CREATE PROCEDURE input script
$ sqlexp -infile hello_world_script.sql example_db 

From the command prompt, you can invoke SQL Explorer and submit the file containing the CREATE PROCEDURE statement as an input script, as shown in Example 9–6.

Example 9–6: CREATE PROCEDURE in context of application call
-- File name: hello_world_script.sql
-- Purpose: Illustrate a CREATE PROCEDURE statement.
@echo true;
@autocommit true;
CREATE PROCEDURE HelloWorld ()
 
BEGIN
     SQLIStatement Insert_HelloWorld = new SQLIStatement (
     "INSERT INTO HelloWorld(fld1) values (’Hello World!’)");
     Insert_HelloWorld.execute();
END
;
COMMIT WORK; 

The Java snippet within the CREATE PROCEDURE statement does not execute as a stand-alone program. Instead, it executes in the context of an application call to the method of the class created by the OpenEdge SQL Engine. This characteristic has the following implications:

Invoking stored procedures

The manner in which applications call stored procedures depends on their environment.

From ODBC

From ODBC, applications use the following ODBC call escape sequence:

Syntax
{ CALL proc_name [ ( parameter [ , ... ] ) ] } ; 

Use parameter markers (question marks used as placeholders) for input or output parameters to the procedure. You can also use literal values for input parameters only. OpenEdge stored procedures do not support return values in the ODBC escape sequence.

Embed the escape sequence in an ODBC SQLExecDirect call to execute the procedure.

Example 9–7 shows a call to a stored procedure named order_parts that passes a single input parameter using a parameter marker.

Example 9–7: Stored procedure passing a single output parameter
SQLUINTEGER Part_num;
SQLINTEGER  Part_numInd = 0;
 
// Bind the parameter.
     SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT,
     SQL_C_SLONG, SQL_INTEGER,
     0, 0, &Part_num, 0, Part_numInd);
 
// Place the department number in Part_num.
Part_num = 318;
// Execute the statement.
SQLExecDirect(hstmt, "{ call order_parts(?) } ", SQL_NTS); 

From JDBC

The JDBC call escape sequence is the same as in ODBC, as shown:

Syntax
{ CALL proc_name [ ( parameter [ , ... ] ) ] } ; 

Embed the escape sequence in a JDBC CallableStatement.prepareCall method invocation.

Example 9–8 shows the JDBC code parallel to the ODBC code excerpt shown in the previous example.

Example 9–8: JDBC stored procedure code
try
{
     CallableStatement statement;
     int Part_num = 318;
     
     // Associate the statement with the procedure call
     // (conn is a previously-instantiated connection object)
     statement = conn.prepareCall("{call order_parts(?)}");
     
     // Bind the parameter.
     statement.setInt(1, Part_num);
     
     // Execute the statement.
     statement.execute();
} 

Modifying and deleting stored procedures

To modify a procedure, you must drop and re-create it. To re-create the procedure, you need the original source of the CREATE PROCEDURE statement. Query system tables to extract the source of the CREATE PROCEDURE statement to a file.

The SQL DROP PROCEDURE statement deletes stored procedures from the database. Exercise care in dropping procedures, since any procedure that calls the dropped procedure will raise an error condition when the now nonexistent stored procedure is invoked.

Stored procedure security

The following guidelines apply to stored procedure security:


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095